package com.demodis.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.demodis.dao.ConResDaoI;
import com.demodis.dao.DetContentDaoI;
import com.demodis.dao.DeterGroupDaoI;
import com.demodis.dao.impl.ConResDaoImpl;
import com.demodis.dao.impl.DetContentDaoImpl;
import com.demodis.dao.impl.DeterGroupDaoImpl;
import com.demodis.domain.TblContRes;
import com.demodis.domain.TblDetContent;
import com.demodis.domain.TblDeterPerson;
import com.demodis.domain.TblUser;
import com.demodis.service.DetContentServiceI;
import com.demodis.util.SystemConstant;

public class DetContentServiceImpl implements DetContentServiceI {

	DetContentDaoI detContDao = new DetContentDaoImpl();
	ConResDaoI conResDao = new ConResDaoImpl();
	DeterGroupDaoI deterDao = new DeterGroupDaoImpl();

	@Override
	public boolean save(TblDetContent t) {
		return detContDao.save(t);
	}

	@Override
	public boolean delete(TblDetContent t) {
		return detContDao.delete(t);
	}

	@Override
	public boolean modify(TblDetContent t) {
		return detContDao.modify(t);
	}

	@Override
	public Object get() {
		List<TblDetContent> list = detContDao.get();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", list.size());
		map.put("rows", list);
		return map;
	}

	@Override
	public Object querySDC(TblDetContent t) {
		return detContDao.querySDC(t);
	}

	@Override
	public Object getContResFroCUser(int userId, int postId, int type) {
		TblContRes contRes = new TblContRes();
		contRes.setUserId(userId);
		List<TblContRes> list = conResDao.get(contRes);
		System.out.println(JSON.toJSONString(list));

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", list.size());
		map.put("rows", list);
		return map;
	}
	
	@Override
	public double getPresidentAndSecretariatAppraise(int userId) {
		return this.detContDao.getPresidentAndSecretariatAppraise(userId);
	}
	
	@Override
	public double getChargeGroupEmployeeAppraiseResult(int userId) {
		return this.detContDao.getChargeGroupEmployeeAppraiseResult(userId);
	}
	
	@Override
	public double getAllTwoLevelLeaderAppraiseResult(int userId) {
		return this.detContDao.getAllTwoLevelLeaderAppraiseResult(userId);
	}

	@Override
	public Map<String, Object> jishuan(TblDeterPerson deterPer) {
		switch (deterPer.getPosId()) {
		case SystemConstant.SCHOOLLEADERPOSID:
			//院领导
			return this.getLeader(deterPer);
		case SystemConstant.ADMINISTRATIONEMPLOYEEPOSID:
			//机关工作人员
			return this.getZhineng(deterPer);
		case SystemConstant.TWOLEVELLEADERPOSID:
			//中层干部
			return this.getZhonggan(deterPer);
		default:
			//其他
			return null;
			//return this.getOrdinary(deterPer);
		}
	}

	@Override
	public Map<String, Object> getLeader(TblDeterPerson deterPer) {
		//分管组所得分数=(分管部门实考核得分数  * 80%
		double groupScore = getDeterManagerGroups(deterPer.getUserId());
		groupScore = groupScore * 0.8;
		//校长书记评价=(校长书记测评所得分数 / 个人得分总分 * 100) * 10%
		double ps = getPresidentAndSecretariatAppraise(deterPer.getUserId());
		ps = (ps / 10 * 100) * 0.1;
		//全院中干=(全院中干测评所得分数 / 个人得分总分 * 100) * 5%
		double tll = getAllTwoLevelLeaderAppraiseResult(deterPer.getUserId());
		tll = (tll / 10 * 100) * 0.05;
		//分管部门员工=(分管部门员工测评所得分数 / 个人得分总分 * 100) * 5%
		double employee = getChargeGroupEmployeeAppraiseResult(deterPer.getUserId());
		employee = (employee / 10 * 100) * 0.05;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("column1Name", "分管部门考核");
		map.put("column1Score", groupScore);
		map.put("column2Name", "校长书记评价");
		map.put("column2Score", ps);
		map.put("column3Name", "所以中干评价");
		map.put("column3Score", tll);
		map.put("column4Name", "分管部门员工评价");
		map.put("column4Score", employee);
		
		return map;
	}

	@Override
	public Map<String, Object> getZhonggan(TblDeterPerson deterPer) {
		//自己组所得分数=(分管部门实际所得分数 / 部门总分 * 100) * 80%
		double groupScore = getZhijiGroupScore(deterPer.getGroupId());
		groupScore = groupScore * 0.8;
		//院领导评价=(院领导测评所得分数 / 个人得分总分 * 100) * 10%
		double ps = getAllFenguanLeaderScore(deterPer.getUserId());
		ps = (ps / 10 * 100) * 0.1;
		//分管部门员工=(分管部门员工测评所得分数 / 个人得分总分 * 100) * 5%
		double employee = getZhijiGroupEmployeeAppraiseResult(deterPer.getUserId(), deterPer.getGroupId());
		employee = (employee / 10 * 100) * 0.05;
		//全院中干=(全院中干测评所得分数 / 个人得分总分 * 100) * 5%
		double tll = getAllTwoLevelLeaderAppraiseResult(deterPer.getUserId());
		tll = (tll / 10 * 100) * 0.05;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("column1Name", "所在部门考核");
		map.put("column1Score", groupScore);
		map.put("column2Name", "院领导评价");
		map.put("column2Score", ps);
		map.put("column3Name", "其他中干");
		map.put("column3Score", tll);
		map.put("column4Name", "自己部门员工评价");
		map.put("column4Score", employee);
		
		return map;
	}

	@Override
	public Map<String, Object> getZhineng(TblDeterPerson deterPer) {
		double deterScore = getDeterScore(deterPer.getUserId());
		deterScore = (deterScore / 10 * 100) * 0.1;
		Map<String,Double> ortherScore = getortherScore(deterPer.getUserId());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("column1Name", "部门考核评价");
		map.put("column1Score", ortherScore.get("groupAppraise") * 0.7);
		map.put("column2Name", "服务满意度");
		map.put("column2Score", deterScore / 10 * 100 * 0.1);
		map.put("column3Name", "述职现场评价");
		map.put("column3Score", ortherScore.get("reciprocalAppraise") * 0.05);
		map.put("column4Name", "述职第一部门负责人评价");
		map.put("column4Score", ortherScore.get("firstGroupAppraise")* 0.15);
		return map;
	}

	@Override
	public double getOrdinary(TblDeterPerson deterPer) {
		return detContDao.getDeterScore(deterPer.getUserId());
	}

	@Override
	public double getDeterManagerGroups(int userId) {
		return deterDao.getDeterManagerGroups(userId);
	}

	@Override
	public double getZhijiGroupScore(int groupId) {
		return detContDao.getZhijiGroupScore(groupId);
	}

	@Override
	public double getAllFenguanLeaderScore(int userId) {
		return detContDao.getAllFenguanLeaderScore(userId);
	}

	@Override
	public double getZhijiGroupEmployeeAppraiseResult(int userId, int groupId) {
		return detContDao.getZhijiGroupEmployeeAppraiseResult(userId, groupId);
	}

	@Override
	public double getDeterScore(int userId) {
		return detContDao.getDeterScore(userId);
	}

	@Override
	public Map<String, Double> getortherScore(int userId) {
		return detContDao.getortherScore(userId);
	}
	
}
